package com.ziyun.pop.web.modules.logistics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.e.logistics.LogisticFinanceStatusEnum;
import com.ziyun.pop.common.e.logistics.LogisticsSendTypeEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.logistics.dto.ErpLogisticsDto;
import com.ziyun.pop.modules.logistics.dto.LogisticsReceiverDto;
import com.ziyun.pop.modules.logistics.dto.LogisticsShipperDto;
import com.ziyun.pop.modules.logistics.dto.TwbLogisticsPaymentDto;
import com.ziyun.pop.modules.logistics.service.ErpLogisticsService;
import com.ziyun.pop.modules.supp.entity.SellerInfoEntity;
import com.ziyun.pop.modules.supp.service.SellerInfoService;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 物流配送表
 *
 * @author llc
 * @email ...
 * @date 2018-09-21 10:12:35
 */
@RestController
@RequestMapping("logistics/erplogistics")
public class ErpLogisticsController {

    @Value("${erpApi}")
    private String erpApi ;
    @Autowired
    private ErpLogisticsService erpLogisticsService;
    @Autowired
    private SellerInfoService sellerInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        return erpLogisticsService.queryPage(params);
    }

    /**
     * 图文宝物流列表
     */
    @RequestMapping("/twbLogisticsList")
    public R twbLogisticsList(@RequestBody Map<String, Object> params){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        return erpLogisticsService.queryPage(params);
    }

    /**
     * 枚举
     */
    @RequestMapping("/enumlist")
    public R enumlist(){
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> logisticsSendTypeEnumList = JsonUtils.enumToList(LogisticsSendTypeEnum.class);
        map.put("logisticsSendTypeEnumList",logisticsSendTypeEnumList);

        List<Map<String, Object>> logisticFinanceStatusList = JsonUtils.enumToList(LogisticFinanceStatusEnum.class);
        map.put("logisticFinanceStatusList",logisticFinanceStatusList);

        List<SellerInfoEntity> merchantList = sellerInfoService.list(new QueryWrapper<>(new SellerInfoEntity()).eq("self" ,1));
        map.put("merchantList",merchantList);

        return R.ok().put("map", map);
    }

    @RequestMapping("/updateLogisticsInfo")
    public R updateLogisticsInfo(@RequestParam("logiId") Long logiId, @RequestParam("logiNo") String logiNo){
        return erpLogisticsService.updateLogisticsInfo(logiId, logiNo);
    }

    @RequestMapping("/updateInfo")
    public R updateInfo(@RequestBody Map<String,Object> map){
        map.put("updateName",UserUtils.getCurentUser().getUsername());
        return erpLogisticsService.updateByMap(map);
    }

    /**
     * 图文宝物流信息批量付款
     */
    @RequestMapping("/twbLogisticsPayment")
    public R twbLogisticsPayment(@RequestBody TwbLogisticsPaymentDto twbLogisticsPaymentDto){
        return erpLogisticsService.twbLogisticsPayment(twbLogisticsPaymentDto);
    }

    /**
     * 录入成本
     */
    @RequestMapping("/enteringCost")
    public R enteringCost(@RequestParam("logiId") Long logiId, @RequestParam("logiCost") double logiCost){
        return erpLogisticsService.enteringCost(logiId,logiCost);
    }

    /**
     * 保存 订单物流信息
     */
    @RequestMapping("/saveOrderLogisticsInfo")
    public R saveOrderLogisticsInfo(@Valid @RequestBody ErpLogisticsDto erpLogisticsDto){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/logistics/erplogistics/saveOrderLogisticsInfo";
        String createName = UserUtils.getCurentUser().getUsername();
        String userId = UserUtils.getCurentUser().getUserId().toString();
        paramBodyMap.put("erpLogisticsDto",JsonUtils.object2String(erpLogisticsDto));
        paramBodyMap.put("createName",createName);
        paramBodyMap.put("userId",userId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 获取物流列表
     */
    @RequestMapping("/getLogiType")
    public R getLogiType(){
        String url = erpApi + "/logistics/erplogistics/queryLogisticTypeList";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = com.utility.JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 快递单寄件收件人信息
     */
    @RequestMapping("/expressFormInfo/{orderNo}")
    public R expressFormInfo(@PathVariable String orderNo){
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String url = erpApi + "/logistics/erplogistics/expressFormInfo";
        paramBodyMap.put("orderNo",orderNo);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = com.utility.JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 修改收货人信息
     */
    @RequestMapping("/saveReceiverInfo")
    public R saveReceiverInfo(@RequestBody LogisticsReceiverDto logisticsReceiverDto){
        return erpLogisticsService.saveReceiverInfo(logisticsReceiverDto);
    }

    /**
     * 修改发货人信息
     */
    @RequestMapping("/saveShipperInfo")
    public R saveShipperInfo(@RequestBody LogisticsShipperDto logisticsShipperDto){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        return erpLogisticsService.saveShipperInfo(logisticsShipperDto, sellerId);
    }


}
